using System;
using System.Data;
using System.Text;

using Schemata.Core;

namespace Awaken.CodeGen
{
	/// <summary>
	/// Summary description for Column.
	/// </summary>
	public class Column
	{
		protected ColumnSchema component;

		public ColumnSchema Component
		{
			get{ return component; }
			set{ component=value; }
		}

		public Column( ColumnSchema columnSchema )
		{
			Component = columnSchema;
		}

		public string ColumnName
		{
			get{ return Component.Name; }
		}

        protected Table Table
        {
            get { return new Table(Component.Table); }
        }
        
        public bool IsPrimaryKey
        {
            get
            {
                return Component.IsPrimaryKeyMember;
            }
        }

        public string GeneratorClass
        {
            get { return this.Table.GeneratorClass; }
        }
   
		public string PropertyName
		{
			get
            {
                StringBuilder sb = new StringBuilder();

                int i=0;
                bool capthis = false;
                bool capnext = false;
                bool add = false;
                foreach (char c in ColumnName)
                {
                    // job_id
                    // JobId

                    if (i == 0)
                    {
                        capthis = true;
                    }

                    if( capnext )
                    {
                        capthis = true;
                        capnext = false;
                    }

                    add = true;

                    if( c.ToString() == "_" )
                    {
                        capnext = true;
                        add = false;
                    }

                    if( capthis && add )
                    {
                        sb.Append(c.ToString().ToUpper());
                    }
                    else if( add )
                    {
                        sb.Append(c.ToString());
                    }

                    capthis = false;

                    i++;
                }

                if (sb.ToString() == Table.ClassName)
                {
                    sb.Append("Column");
                }
                
                return sb.ToString();            
            }
		}

        public string DataType
        {
            get { return Component.DataType.ToString(); }
        }

		public string CSharpTypeName
		{
			get
			{
				string result = string.Empty;

				switch( Component.DataType )
				{
					case( DbType.Boolean )	: result = "bool?"; break;
                    case (DbType.Byte)      : result = "byte"; break;
                    case( DbType.Binary )	: result = "byte[]"; break;
					case( DbType.Currency )	: result = "decimal?"; break;
					//case( DbType.Date )		: result = "NullableDateTime"; break;
					//case( DbType.DateTime )	: result = "NullableDateTime"; break;
                    case( DbType.Date )		: result = "DateTime?"; break;
                    case( DbType.DateTime ) : result = "DateTime?"; break;
					case( DbType.Decimal )	: result = "decimal?"; break;
					case( DbType.Double )	: result = "double?"; break;					
					case( DbType.Guid )		: result = "Guid?"; break;
					case( DbType.Int16 )	: result = "System.Int16?"; break;
					case( DbType.Int32 )	: result = "System.Int32?"; break;
                    case (DbType.AnsiString): result = "string"; break;
                    case( DbType.AnsiStringFixedLength ) : result = "string"; break;
                    case( DbType.String )	: result = "string"; break;
					case( DbType.StringFixedLength ) : result = "string"; break;
					default: result= "(undefined:" +Component.DataType.ToString() + ")" ; break;
				}

				return result;
			}
		}

		public string NHibernateTypeName
		{
			get
			{
				string result = string.Empty;

				switch( Component.DataType )
				{
					case( DbType.Boolean )	: result = "Boolean"; break;
                    case (DbType.Byte)      : result = "Byte"; break;
                    case (DbType.Binary)    : result = "BinaryBlob"; break;
                    case (DbType.Currency)  : result = "Decimal"; break;
                    //case (DbType.Date)      : result = "Nullables.NHibernate.NullableDateTimeType, Nullables.NHibernate"; break;
                    //case (DbType.DateTime)  : result = "Nullables.NHibernate.NullableDateTimeType, Nullables.NHibernate"; break;
                    case (DbType.Date)      : result = "DateTime"; break;
                    case (DbType.DateTime)  : result = "DateTime"; break;
                    case (DbType.Decimal)   : result = "Decimal"; break;
                    case (DbType.Double)    : result = "Double"; break;
                    case (DbType.Guid)      : result = "Guid"; break;
                    case (DbType.Int16)     : result = "Int16"; break;
                    case (DbType.Int32)     : result = "Int32"; break;
                    case (DbType.AnsiString): result = "String"; break;
                    case (DbType.AnsiStringFixedLength): result = "String"; break;
                    case (DbType.String)    : result = "String"; break;
                    case (DbType.StringFixedLength): result = "String"; break;
					default: result= "(undefined:" +Component.DataType.ToString() + ")" ; break;
				}

				return result;
			}
		}
	}
}
